//
//  SLChatTextCell.m
//  MyChatDemo
//
//  Created by DragonYao on 2017/9/21.
//  Copyright © 2017年 DragonYao. All rights reserved.
//

#import "SLChatTextCell.h"

static CGFloat kNameLabelH = 14.0;
static CGFloat kFontSize = 13.0;

@interface SLChatTextCell ()


@end

@implementation SLChatTextCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        //文本内容
        self.contextLabel = [UILabel new];
        self.contextLabel.textColor = SLColor(17, 17, 17, 1);
        self.contextLabel.font = [UIFont systemFontOfSize:kFontSize];
        self.contextLabel.numberOfLines = 0;
        self.contextLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:self.contextLabel];
    }
    return self;
}

//重新布局控件
- (void)layoutFrameWithText:(NSString *)text
               withNickname:(NSString *)nickname
                fromFriends:(BOOL)isFriend {
    
    NSDictionary *attrDic = @{NSFontAttributeName:[UIFont systemFontOfSize:kFontSize], NSForegroundColorAttributeName:SLColor(17, 17, 17, 1)};
    CGFloat nicknameW = (kWIDTH - kPadding * 4 - kHeadW * 2);
    CGFloat contentW = kWIDTH - kPadding * 6 - kHeadW * 2 - kTriWidth;
    CGRect textRect = [text boundingRectWithSize:CGSizeMake(contentW, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDic context:nil];
    CGFloat bubbleImgW = (textRect.size.width + kPadding * 2 + kTriWidth);
    if (isFriend) {//文本靠左
        self.headImgView.frame = CGRectMake(kPadding, kPadding, kHeadW, kHeadH);
        self.headImgView.image = [UIImage imageNamed:@"friendAvertor"];
        self.nicknameLabel.frame = CGRectMake(kHeadW + kPadding *2, kPadding, nicknameW, kNameLabelH);
        self.nicknameLabel.textAlignment = NSTextAlignmentLeft;
        //文本内容
        self.bgImgView.frame = CGRectMake(kHeadW + kPadding *2, self.nicknameLabel.bottom + kPadding, bubbleImgW, (textRect.size.height + kPadding * 2));
        self.bgImgView.image = [[UIImage imageNamed:@"chatfrom_bg_normal"] stretchableImageWithLeftCapWidth:25 topCapHeight:25];
        self.contextLabel.frame = CGRectMake(kHeadW + kPadding *2 + kTriWidth, self.bgImgView.top + kPadding, textRect.size.width, textRect.size.height);
    }
    else {//文本靠右
        self.headImgView.frame = CGRectMake((kWIDTH - kHeadW - kPadding), kPadding, kHeadW, kHeadH);
        self.headImgView.image = [UIImage imageNamed:@"meAvertor"];
        self.nicknameLabel.frame = CGRectMake(kHeadW + kPadding *2, kPadding, nicknameW, kNameLabelH);
        self.nicknameLabel.textAlignment = NSTextAlignmentRight;
        //文本内容
        CGFloat bgImgOffsetX = kWIDTH - (kHeadW + kPadding * 2 + bubbleImgW);
        self.bgImgView.frame = CGRectMake(bgImgOffsetX, self.nicknameLabel.bottom + kPadding, bubbleImgW, (textRect.size.height + kPadding * 2));
        self.bgImgView.image = [[UIImage imageNamed:@"chatto_bg_normal"] stretchableImageWithLeftCapWidth:25 topCapHeight:25];
        self.contextLabel.frame = CGRectMake((self.bgImgView.left + kPadding), self.bgImgView.top + kPadding, textRect.size.width, textRect.size.height);
    }
    
    self.nicknameLabel.text = nickname;
    self.contextLabel.text = text;
}

+ (CGFloat)cellHeight:(XMPPMessageArchiving_Message_CoreDataObject *)msg
{
    CGFloat contentW = kWIDTH - kPadding * 6 - kHeadW * 2 - kTriWidth;
    NSDictionary *dict = @{NSFontAttributeName: [UIFont systemFontOfSize:kFontSize]};
    CGFloat textHeight = [msg.body boundingRectWithSize:CGSizeMake(contentW, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
    CGFloat cellHeight = (textHeight + kPadding * 5 + kNameLabelH) < 60.0 ? 60.0 : (textHeight + kPadding * 5 + kNameLabelH);
    return cellHeight;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)showHeadImage:(UITapGestureRecognizer *)tap {
    SLog(@"点击了头像");
}

@end
